package com.gdust.lease.web.admin.controller.apartment;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gdust.lease.common.result.Result;
import com.gdust.lease.model.entity.FeeKey;
import com.gdust.lease.model.entity.FeeValue;
import com.gdust.lease.web.admin.service.FeeKeyService;
import com.gdust.lease.web.admin.service.FeeValueService;
import com.gdust.lease.web.admin.vo.fee.FeeKeyVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 房间杂费管理接口
 */
@Tag(name = "房间杂费管理")
@RestController
@RequestMapping("/admin/fee")
public class FeeController {
    //注入杂项费用名称表的service
    @Autowired
    private FeeKeyService feeKeyService;

    //注入杂项费用值表的service
    @Autowired
    private FeeValueService feeValueService;

    /**
     * 保存或更新杂费名称
     * @param feeKey 杂项费用名称表，内含：付款项key
     * @return
     */
    @Operation(summary = "保存或更新杂费名称")
    @PostMapping("key/saveOrUpdate")
    public Result saveOrUpdateFeeKey(@RequestBody FeeKey feeKey) {
        //调用Mybatis-Plus提供的封装方法，保存或更新杂费名称
        feeKeyService.saveOrUpdate(feeKey);
        return Result.ok();
    }

    /**
     * 保存或更新杂费值
     * @param feeValue 费用value，内含：费用value和收费单位和费用所对的fee_key编码
     * @return
     */
    @Operation(summary = "保存或更新杂费值")
    @PostMapping("value/saveOrUpdate")
    public Result saveOrUpdateFeeValue(@RequestBody FeeValue feeValue) {
        //调用Mybatis-Plus提供的封装方法，保存或更新杂费值
        feeValueService.saveOrUpdate(feeValue);
        return Result.ok();
    }

    /**
     * 查询全部杂费名称和杂费值列表
     *
     * FeeKeyVo内含：feeValueList(杂费value列表),FeeValue(杂项费用值表)内含：费用value、收费单位、费用所对的fee_key编码
     *
     * @return
     */
    @Operation(summary = "查询全部杂费名称和杂费值列表")
    @GetMapping("list")
    public Result<List<FeeKeyVo>> feeInfoList() {
        //自定义查询：查询全部杂费名称和杂费值列表
        List<FeeKeyVo> list = feeKeyService.listFeeInfo();
        return Result.ok(list);
    }

    /**
     * 根据id删除杂费名称
     *
     * 注意：注意删除杂费名称时，会一并删除其下的所有杂费值
     *
     * @param feeKeyId 传入的杂费id
     * @return
     */
    @Operation(summary = "根据id删除杂费名称")
    @DeleteMapping("key/deleteById")
    public Result deleteFeeKeyById(@RequestParam Long feeKeyId) {
        //1.删除FeeKey属性名
        feeKeyService.removeById(feeKeyId);

        //2.删除关联的FeeValue属性值
        LambdaQueryWrapper<FeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //匹配杂费值表中feeKeyId字段等于传入的feeKeyId
        feeValueLambdaQueryWrapper.eq(FeeValue::getFeeKeyId,feeKeyId);
        //调用Mybatis-Plus提供的封装方法，删除满足条件的FeeValue属性值
        feeValueService.remove(feeValueLambdaQueryWrapper);

        return Result.ok();
    }

    /**
     *根据id删除杂费值
     * @param id 传入id
     * @return
     */
    @Operation(summary = "根据id删除杂费值")
    @DeleteMapping("value/deleteById")
    public Result deleteFeeValueById(@RequestParam Long id) {
        //调用Mybatis-Plus提供的封装方法，根据id删除杂费值
        feeValueService.removeById(id);
        return Result.ok();
    }
}
